package com.samapp.settings;

import java.util.Currency;

import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.base.BaseActivity;
import com.samapp.entity.User;

public class ChangePasswordActivity extends BaseActivity {

	/** 用户名 */
	@ViewInject(R.id.username)
	TextView textviewUserName;
	private String userName;
	/** 旧密码 */
	@ViewInject(R.id.password)
	EditText edittextPassWord;
	private String passWord;
	/** 新密码 */
	@ViewInject(R.id.new_password)
	EditText edittextNewPassWord;
	private String newPassWord;
	private MyApplication myApplication = MyApplication.getApplicationInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		ViewUtils.inject(this);
		userName = myApplication.currentUser.getUserName();
		textviewUserName.setText(userName);
	}

	@OnClick(R.id.btn_submit)
	public void onBtnSubmitClick(View view) {
		passWord = edittextPassWord.getText().toString().trim();
		newPassWord = edittextNewPassWord.getText().toString().trim();
		if ("".equals(passWord)) {
			showToast("旧密码不能为空");
		} else if ("".equals(newPassWord)) {
			showToast("新密码不能为空");
		} else if (passWord.equals(newPassWord)) {
			showToast("新旧密码不能一样");
		} else {
			changePassword();
		}
	}

	@OnClick(R.id.btn_back)
	public void onBtnBackClick(View view) {
		finish();
	}

	/**
	 * 修改密码
	 */
	private void changePassword() {
		showLoadingDialog("修改密码中……");
		String url = myApplication.url + "editPwd.htm";
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("accessToken", "5C9.yx0S");
		params.addQueryStringParameter("userName", userName);
		params.addQueryStringParameter("passWord", passWord);
		params.addQueryStringParameter("newPassWord", newPassWord);
		new HttpUtils().send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.i("Http", responseInfo.result);
				// {"code":"0","msg":"修改成功"}
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					if (jsonObject.has("code") && "0".equals(jsonObject.getString("code"))) {
						showToast("修改成功");
					} else {
						showToast(jsonObject.getString("msg"));
					}
					dismissLoadingDialog();
				} catch (Exception e) {
					e.printStackTrace();
					dismissLoadingDialog();
				}
			}

			@Override
			public void onFailure(HttpException e, String arg1) {
				e.printStackTrace();
				dismissLoadingDialog();
			}
		});
	}
}
